﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UniRlv.Utility;

namespace UniRlv.UI.Main.SystemManage
{
    public partial class FrmSystemSetting : UniRlv.UI.Main.FrmBase
    {
        public FrmSystemSetting()
        {
            InitializeComponent();
        }

        private void FrmSystemSetting_Load(object sender, EventArgs e)
        {
            txtUnit.Text = ConfigurationHelper.AppGetString("unit");
            txtCodeReg.Text = ConfigurationHelper.AppGetString("codeReg");
            lstPage.Text = ConfigurationHelper.AppGetString("page");
            chkCheckTymPzwh.Checked = ConfigurationHelper.AppGetBoolean("checkProduct");
            chkCheckTymPzwhLevel1.Checked = ConfigurationHelper.AppGetBoolean("checkProductLevel1");

            if (!ConfigurationHelper.HasSetting("checkDataQty"))
            {
                ConfigurationHelper.AppSet("checkDataQty", "50");//默认50万
            }
                
            numCheckDataQty.Value = ConfigurationHelper.AppGetInt("checkDataQty");

            if (!ConfigurationHelper.HasSetting("printLackQty"))
            {
                ConfigurationHelper.AppSet("printLackQty", "50");//默认50条
            }
                
            numPrintLackQty.Value = ConfigurationHelper.AppGetInt("printLackQty");


            
            if (!ConfigurationHelper.HasSetting("autoSaveTimeSpan"))
            {
                ConfigurationHelper.AppSet("autoSaveTimeSpan", "5");//默认1分钟 ，改为5秒钟
            }

            numAutoSaveTimeSpan.Value = ConfigurationHelper.AppGetInt("autoSaveTimeSpan") < 5?5: ConfigurationHelper.AppGetInt("autoSaveTimeSpan");



            if (!ConfigurationHelper.HasSetting("pickTwice"))
            {
                ConfigurationHelper.AppSet("pickTwice", "true");
            }

            chkPickTwice.Checked = ConfigurationHelper.AppGetBoolean("pickTwice");



            if (!ConfigurationHelper.HasSetting("voiceTipLevel1"))
            {
                ConfigurationHelper.AppSet("voiceTipLevel1", "false");
            }

            chkVoiceLevel1.Checked = ConfigurationHelper.AppGetBoolean("voiceTipLevel1");



            if (!ConfigurationHelper.HasSetting("voiceTip"))
            {
                ConfigurationHelper.AppSet("voiceTip", "false");
            }

            chkVoice.Checked = ConfigurationHelper.AppGetBoolean("voiceTip");

            if (!ConfigurationHelper.HasSetting("dbBKPeriod"))
            {
                ConfigurationHelper.AppSet("dbBKPeriod", "90");//默认90天
            }

            numBKPeriod.Value = ConfigurationHelper.AppGetInt("dbBKPeriod");

            if (!ConfigurationHelper.HasSetting("bAutoUpdate"))
            {
                ConfigurationHelper.AppSet("bAutoUpdate", "false");//默认不自动跟新
            }

            chkAutoUpdate.Checked = ConfigurationHelper.AppGetBoolean("bAutoUpdate");


            if (!ConfigurationHelper.HasSetting("outputInFileName"))
            {//导出入库文件命名规则
                ConfigurationHelper.AppSet("outputInFileName", "{itemid}_共计{qty}{unit}{tym}_{batch}_{taskid}");
            }

            txtInFileName.Text = ConfigurationHelper.AppGetString("outputInFileName");
            lbTip.Text = $"{{itemid}}=商品编码，{{qty}}=一级码数量，{{unit}}=最小贴码单位，{{tym}}=通用名，{{batch}}=批次号，\r\n{{taskid}}=任务号，{{taskNO}}=任务编码，{{gg}}=规格，{{spm}}=商品名，{{now}}=导出时间";

            if (!ConfigurationHelper.HasSetting("logClrPeriod"))
            {
                ConfigurationHelper.AppSet("logClrPeriod", "90");//默认90天
            }

            numLogClrPeriod.Value = ConfigurationHelper.AppGetInt("logClrPeriod");

            if (!ConfigurationHelper.HasSetting("proccessClrPeriod"))
            {
                ConfigurationHelper.AppSet("proccessClrPeriod", "3");//默认3天
            }

            numProccessClrPeriod.Value = ConfigurationHelper.AppGetInt("proccessClrPeriod");

            if (!ConfigurationHelper.HasSetting("pickBingding"))
            {
                ConfigurationHelper.AppSet("pickBingding", "false");
            }

            chkPickBind.Checked = ConfigurationHelper.AppGetBoolean("pickBingding");

            if (!ConfigurationHelper.HasSetting("dupCheckType"))
            {
                ConfigurationHelper.AppSet("dupCheckType", "0");
            }

            lstDupCheckType.SelectedIndex = ConfigurationHelper.AppGetInt("dupCheckType");

            CodePrcUtility.PRODUCT_CHECKLevel1 = chkCheckTymPzwhLevel1.Checked;
            CodePrcUtility.PRODUCT_CHECK = chkCheckTymPzwh.Checked;
            CodePrcUtility.PICK_TWICE = chkPickTwice.Checked;
        }

        private void txtUnit_TextChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("unit", txtUnit.Text);
        }

        private void lstPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("page", lstPage.Text);
        }

        private void chkCheckTymPzwh_CheckedChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("checkProduct", chkCheckTymPzwh.Checked.ToString());
            CodePrcUtility.PRODUCT_CHECK = chkCheckTymPzwh.Checked;
        }

        private void chkCheckTymPzwhLevel1_CheckedChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("checkProductLevel1", chkCheckTymPzwhLevel1.Checked.ToString());
            CodePrcUtility.PRODUCT_CHECKLevel1 = chkCheckTymPzwhLevel1.Checked;
        }

        private void numCheckDataQty_ValueChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("checkDataQty", numCheckDataQty.Value.ToString());
        }

        private void chkPickTwice_CheckedChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("pickTwice", chkPickTwice.Checked.ToString());
            CodePrcUtility.PICK_TWICE = chkPickTwice.Checked;
        }

        private void numPrintLackQty_ValueChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("printLackQty", numPrintLackQty.Value.ToString());
        }

        private void chkVoice_CheckedChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("voiceTip", chkVoice.Checked.ToString());
        }

        private void numAutoSaveTimeSpan_ValueChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("autoSaveTimeSpan", numAutoSaveTimeSpan.Value.ToString());
        }

        private void chkVoiceLevel1_CheckedChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("voiceTipLevel1", chkVoiceLevel1.Checked.ToString());
        }

        private void numBKPeriod_ValueChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("dbBKPeriod", numBKPeriod.Value.ToString());
        }

        private void chkAutoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("bAutoUpdate", chkAutoUpdate.Checked.ToString());
        }

        private void txtInFileName_TextChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("outputInFileName", txtInFileName.Text);
        }

        private void numLogClrPeriod_ValueChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("logClrPeriod", numLogClrPeriod.Value.ToString());
        }

        private void numProccessClrPeriod_ValueChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("proccessClrPeriod", numProccessClrPeriod.Value.ToString());
        }

        private void cbPickBind_CheckedChanged(object sender, EventArgs e)
        {
            ConfigurationHelper.AppSet("pickBingding", chkPickBind.Checked.ToString());
        }

        private void lstDupCheckType_SelectedIndexChanged(object sender, EventArgs e)
        {
            numCheckDataQty.Enabled = lstDupCheckType.SelectedIndex == 0;
            ConfigurationHelper.AppSet("dupCheckType", lstDupCheckType.SelectedIndex.ToString());
        }
    }
}
